package gorsa

import (
	"crypto/sha1"
	"encoding/base64"
	"encoding/hex"
	"fmt"
	"fx_swift_schedule/pkg/global"
)

// PublicEncrypt 公钥加密
func PublicEncrypt(data, publicKey string) (string, error) {
	baseData := base64.StdEncoding.EncodeToString([]byte(data))

	gRsa := RSASecurity{}
	gRsa.SetPublicKey(publicKey)

	rsaData, err := gRsa.PubKeyENCTYPT([]byte(baseData))
	if err != nil {
		return "", err
	}

	return hex.EncodeToString([]byte(rsaData)), nil
}

// PriKeyEncrypt 私钥加密
func PriKeyEncrypt(data, privateKey string) (string, error) {

	gRsa := RSASecurity{}
	gRsa.SetPrivateKey(privateKey)
	baseData := base64.StdEncoding.EncodeToString([]byte(data))

	rsaData, err := gRsa.PriKeyENCTYPT([]byte(baseData))
	if err != nil {
		return "", err
	}

	return hex.EncodeToString([]byte(rsaData)), nil
}

// PublicDecrypt 公钥解密
func PublicDecrypt(data, publicKey string) (string, error) {
	dataByte, err := hex.DecodeString(data)
	if err != nil {
		return "", err
	}

	gRsa := RSASecurity{}
	if err := gRsa.SetPublicKey(publicKey); err != nil {
		return "", err
	}
	rsaData, err := gRsa.PubKeyDECRYPT(dataByte)
	if err != nil {
		return "", err
	}
	dataBs, err := base64.StdEncoding.DecodeString(string(rsaData))
	if err != nil {
		return "", err
	}
	return string(dataBs), nil
}

// PriKeyDecrypt 私钥解密
func PriKeyDecrypt(data, privateKey string) (string, error) {
	dataByte, err := hex.DecodeString(data)
	if err != nil {
		return "", err
	}

	gRsa := RSASecurity{}

	if err := gRsa.SetPrivateKey(privateKey); err != nil {
		return "", err
	}

	rsaData, err := gRsa.PriKeyDECRYPT(dataByte)
	if err != nil {
		return "", err
	}

	dataBs, _ := base64.StdEncoding.DecodeString(string(rsaData))

	return string(dataBs), nil
}

// PriKeySignSha1 私钥sha1签名
func PriKeySha1Sign(data, privateKey string) (string, error) {
	sha1Hash := sha1.New()
	sha1Hash.Write([]byte(data))
	dataH := hex.EncodeToString(sha1Hash.Sum(nil))
	fmt.Println("sha1摘要结果：", dataH)

	gRsa := RSASecurity{}
	if err := gRsa.SetPrivateKey(privateKey); err != nil {
		return "", err
	}

	rsaData, err := gRsa.SignSha1WithRsa(dataH)
	if err != nil {
		return "", err
	}

	return string(rsaData), nil
}

// PubKeySha1Verify 公钥验签
func PubKeySha1Verify(data string, signData string, publicKey string) error {
	sha1Hash := sha1.New()
	sha1Hash.Write([]byte(data))
	dataH := hex.EncodeToString(sha1Hash.Sum(nil))
	fmt.Println("sha1摘要结果：", dataH)

	gRsa := RSASecurity{}
	if err := gRsa.SetPublicKey(publicKey); err != nil {
		return err
	}

	if err := gRsa.VerifySignSha1WithRsa(dataH, signData); err != nil {
		return err
	}
	fmt.Println("公钥验签结果：succeeded")
	return nil
}

func GetPrivateKeyStr() string {
	privateKeyPEM := fmt.Sprintf("-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----", global.ViperObj.GetString("gep.privateKey"))
	return privateKeyPEM
}

func GetPublicKeyStr() string {
	publicKeyPEM := fmt.Sprintf("-----BEGIN RSA PRIVATE KEY-----\n%s\n-----END RSA PRIVATE KEY-----", global.ViperObj.GetString("gep.pubkey"))
	return publicKeyPEM
}
